<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Model;

class QuestionsOrderManageController extends Controller {
    public function __construct()
    {
        parent::__construct();
        if(!logAdmin(session('adminId'))) header('location:'.U('Admin/Index/login'));
    }
    
    public function index(){
         
    }
     
    public function Order_list(){
        if(IS_GET){
            $result=D('QuestionsOrder')->Order_List();
            $money=D('QuestionsOrder')->Count_money();
            $this->assign('money',$money);
            $this->assign('Order_List',$result['list']);
            $this->assign('page',$result['show']);
            $this->display('OrderManage/QuestionsOrder_list');
             
            //          switch ($len){
            //              case 2:
            //                  if($type[0]==1){
    
            //                  }                //显示书籍订单列表页面
            //                  else  $this->display('OrderManage/OtherOrder_list'); break;   //显示电子资料、题库、押密流水列表页面
             
            //          }
        }
    }
    
   public function check_userInfo(){
        if(IS_POST){
            $userId=I('post.userId');
            $username=M('user')->field('name')->where(['id'=>$userId])->find();
            $this->ajaxReturn('用户真实姓名:'.$username['name']);
        }
    }
    
    public function add_order(){
        if(IS_POST){
            $data=I('post.');
            $user_id=$data['user_id'];
            $cat_id=$data['cat_id'];
            $order_id=date('Ymdhis',time()).rand(1000,9999);
            $price=M('price')->field('exam_price')->where(['cate_id'=>$cat_id])->find();
            $addtime=time();
            $data['user_id']=$user_id;
            $data['cat_id']=$cat_id;
            $data['order_id']=$order_id;
            $data['price']=$price['exam_price'];
            $data['addtime']=$addtime;
            $data['status']=1;
             
            M('user')->where(['id'=>$data['user_id']])->find()?'':$this->error('用户不存在');
    
            $data=D('QuestionsOrder')->create($data);
            if($data){
                $result=D('QuestionsOrder')->add_order($data);
                $result?$this->success('添加成功',U('Order_list')):$this->error('添加失败');
            }else{
                $this->error(D('QuestionsOrder')->getError(),U('Order_list'));
            }
        }else{
            $cate_list=M('category')->where(['level'=>2])->select();
            $this->assign('cat_list',$cate_list);
            $this->display('OrderManage/AddQuestionsOrder');
        }
    }
    
    public function Delete_order(){
        if(IS_POST){
            $info=I('post.');
            if(!empty($info)){
                $result=D('QuestionsOrder')->delete_order($info);
                $dele=$result['delCount']?true:false;
                $money=$result['money'];
                $data['delCount']=$dele;
                $data['money']=$money;
                $this->ajaxReturn($data);
            }else{
                $this->ajaxReturn('提交信息出错');
            }
        }
    }
     
    public function Delete_all(){
        if(IS_POST){
            $info=I('post.');
            if(!empty($info)){
                $result=D('QuestionsOrder')->delete_all($info);
                $dele=$result['delCount']?true:false;
                $money=$result['money'];
                $data['delCount']=$dele;
                $data['money']=$money;
                $this->ajaxReturn($data);
            }else{
                $this->ajaxReturn('提交信息出错');
            }
        }
    }
     
    /*
     * 进行订单的筛选查询，order_type为订单类型。数据库中默认的订单日期为 2000-01-01的时间戳
     */
     
    public function query_order(){
        if(IS_GET){
            $data=I('get.');
            if(!empty($data)){
                $list=D('QuestionsOrder')->query_order($data);
                if($list['list']){
                    $this->assign('page',$list['show']);
                    $this->assign('money',$list['money']);
                    $this->assign('Order_List',$list['list']);
                    $this->display('OrderManage/QuestionsOrder_list');
                }
                else{
                    $this->error('未查找到满足条件的数据');
                }
            }
        }
    }
}